package com.ynsj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynsj.mapper.NoticeMapper;
import com.ynsj.domain.Notice;
import com.ynsj.service.NoticeService;
import org.springframework.util.StringUtils;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService{

    /**
     * 分页查询公告
     *
     * @param noticePage
     * @param title
     * @param startTime
     * @param endTime
     * @param status
     * @return
     */
    @Override
    public Page<Notice> findByPage(Page<Notice> noticePage, String title, String startTime, String endTime, Integer status) {
        noticePage.addOrder(OrderItem.desc("last_update_time"));
        return page(noticePage,new LambdaQueryWrapper<Notice>()
                .like(!StringUtils.isEmpty(title),Notice::getTitle,title)
                .between(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime),Notice::getCreated,startTime,endTime+" 23:59:59")
                .eq(status!=null,Notice::getStatus,status));
    }

    /**
     * 启用/禁用一个公告
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean updateStatus(Long id, Integer status) {
        return update(new LambdaUpdateWrapper<Notice>()
                .eq(id!=null,Notice::getId,id)
                .set(status!=null,Notice::getStatus,status));
    }
}
